package com.zhihuitong.meeting.meeting.list;

import android.util.Log;

import com.zhihuitong.meeting.base.BasePresenter;
import com.zhihuitong.meeting.dao.SQLiteUtils;

import org.litepal.crud.callback.FindMultiCallback;

import java.util.List;

/**
 * 作者： guoyuan
 * 时间 2020/2/24.
 * 文件描述：
 * 版本号：V 1.0
 **/
public class MeetingListPresenter extends BasePresenter<MeetingListContract.IMeetingListView> implements MeetingListContract.IMeetingListPresenter {
    public MeetingListPresenter(MeetingListContract.IMeetingListView view) {
        super(view);
    }

    private int offset = 0;

    @Override
    public void loadData(final boolean loadMore) {
        if (loadMore) {
            offset += 20;
        } else {
            offset = 0;
        }
        try {
            SQLiteUtils.findAllMsgAsync(offset, new FindMultiCallback() {
                @Override
                public void onFinish(List list) {
                    Log.i("DB_Meeting", list.toString());
                    if (loadMore) {
                        if (list == null || list.isEmpty()) {
                            getView().noMore();
                        } else {
                            getView().onMoreData(list);
                        }
                    } else {
                        if (list == null || list.isEmpty()) {
                            getView().onDataEmpty();
                        } else {
                            getView().onNewData(list);
                        }
                    }
                }
            });
        } catch (Exception e) {
            if (loadMore) {
                getView().loadMoreError("查询更多数据报错");
            } else {
                getView().loadMoreError("查询数据报错");
            }
            e.printStackTrace();
        }
    }
}
